<?php

class private_messages extends cms_controller {

//put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('private_model', 'private');
        $this->load->helper('file');
    }

    public function index($type = false, $limit = FALSE, $order = FALSE) {
        if (in_array($type, array('inbox', 'outbox'))) {
            $this->pass['ptype'] = $type;
            switch ($type) {
                case 'inbox':
                    $where = array('receiver_id' => $this->user_id, 'receiver_trash' => 'NO');
                    $order = array('seen' => 'desc');
                    break;
                case 'outbox':
                    $where = array('sender_id' => $this->user_id, 'sender_trash' => 'NO');
                    $order = FALSE;
                    break;
                default :
                    redirect(__CLASS__ . '/index/inbox');
                    break;
            }
            $this->pass['messages'] = $this->private->get($where, FALSE, 'result', FALSE, FALSE, false, $order);
            $this->themes->render('private/index', $this->pass);
            return;
        }
        redirect(__CLASS__ . '/index/inbox');
    }

    public function send($id = false) {
        if (is_numeric($id)) {
            $this->load->model('user_model');
            $this->user_model->is_found(array('id' => $id), array(), 'row', __CLASS__);
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->form_validation->set_rules('subject', lang('label_subject'), 'required|trim');
                $this->form_validation->set_rules('messages', lang('label_message'), 'required|trim');
                if ($this->form_validation->run()) {
                    $save['subject'] = $this->input->post('subject');
                    $save['message'] = $this->input->post('messages');
                    $save['receiver_id'] = $id;
                    $save['sender_id'] = $this->user_id;
                    if ($mid = $this->private->save($save)) {
                        $this->session->unset_userdata('post');
                        $this->themes->set_message(array('url' => __CLASS__."/index/view/$mid", 'type' => 'success', 'message' => 'message_add_success'));
                    } else {
                        get_save_error();
                    }
                } else {

                    get_validation_errors();
                }
            }
            $this->pass['userid'] = $id;
            $this->themes->render('private/send', $this->pass);
            return;
        }
        redirect(__CLASS__);
    }

    public function view($id = false) {
        if (is_numeric($id)) {
            $id_id = $this->private->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $type = false;
            if ($id_id->sender_id == $this->user_id && $id_id->sender_trash == 'NO') {
                $type = 'outbox';
            } elseif ($id_id->receiver_id == $this->user_id && $id_id->receiver_trash == 'NO') {
                $type = 'inbox';
                if ($id_id->seen == 'NO') {
                    $save['seen'] = 'YES';
                    $save['seen_date'] = date("Y-m-d H:i:s");
                    $this->private->save($save, array('id' => $id), TRUE);
                    $id_id = $this->private->is_found(array('id' => $id), array(), 'row', __CLASS__);
                }
            } else {
                redirect(__CLASS__ . '/index/inbox');
                return;
            }

            if ($type) {
                $this->pass['message'] = $id_id;

                $this->pass['ptype'] = $type;
                $this->themes->render(
                        'private/view', $this->pass);
                return;
            }
        }

        redirect(__CLASS__ . '/index/inbox');
    }

    public function delete($id = false) {
        if (is_numeric($id)) {
            $id_id = $this->private->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $save = false;
            $type = false;
            if ($id_id->sender_id == $this->user_id) {
                $save['sender_trash'] = 'YES';
                $type = 'outbox';
            } elseif ($id_id->receiver_id == $this->user_id) {
                $save['receiver_trash'] = 'YES';
                $type = 'inbox';
                if ($id_id->seen == 'NO') {
                    $this->themes->set_message(array('url' => __CLASS__ . '/index/' . $type, 'type' => 'error', 'message' => 'message_delete_error_not_seen'));

                    return;
                }
            } else {
                redirect(__CLASS__ . '/index/inbox');
                return;
            }
            if ($save) {
                $this->private->save($save, array('id' => $id), TRUE);
                $this->themes->set_message(array('url' => __CLASS__ . '/index/' . $type, 'type' => 'success', 'message' => 'message_delete_success'));
                return;
            }
        }
        redirect(__CLASS__ . '/index/inbox');
    }

}